package com.smartTerminal.perMatters.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.smartTerminal.perMatters.entity.Site;
import com.smartTerminal.perMatters.entity.SiteItem;
import com.smartTerminal.perMatters.service.SiteService;
/**
 * 部门办事
 * @author wantao
 * @Date 2018年1月19日
 *
 */
@Controller
public class SiteController {

	@Autowired
	private SiteService siteService;
	
	/**
	 * 获取所有部门
	 * @param request
	 * @return 部门信息
	 */
	@RequestMapping("getSiteList")
	public String getSiteList(HttpServletRequest request){
		List<Site> list = null;
		String data = siteService.getSiteAndItemList("", 0);
		if(data!=null){
			list = JSONArray.parseArray(data,Site.class);
		}else{
			request.setAttribute("tips", "数据错误，请返回重试");
			return "permatters/error";
		}
		request.setAttribute("siteList", list);
		return "permatters/siteList";
	}
	
	/**
	 * 获取所有部门事项
	 * @param request
	 * @param siteName 单位名称
	 * @param currentpage 分页(当前页码)
	 * @param pagesize 分页大小
	 * @return
	 */
	@RequestMapping("getSiteItemList")
	public String getSiteItemList(HttpServletRequest request,String siteName,String currentpage,String pagesize){
		String data = siteService.getSiteItemList(siteName);
		List<SiteItem> itemsList = null;
		List<SiteItem> pageItemsList = new ArrayList<>();
		if(data!=null){
			itemsList = JSON.parseArray(data,SiteItem.class);//所有事项列表数据 
			if(itemsList.size()<1){
				request.setAttribute("tips", "该部门下暂无网上办理事项");
				return "user/error";
			}
			int begin = Integer.parseInt(pagesize)*(Integer.parseInt(currentpage)-1);
			int end = 0;
			if(Integer.parseInt(pagesize)*(Integer.parseInt(currentpage))>itemsList.size()-1){
				end = itemsList.size()-1;
			}else{
				end = Integer.parseInt(pagesize)*(Integer.parseInt(currentpage))-1;
			}
			for(int i=begin; i<=end;i++){
				pageItemsList.add(itemsList.get(i));
			}
			int currentRows =(int) Math.ceil(Double.parseDouble(Integer.toString(itemsList.size()))/Double.parseDouble(pagesize));
			SiteItem siteItem = new SiteItem();
			siteItem.setCurrentpage(Integer.parseInt(currentpage));
			siteItem.setPagesize(Integer.parseInt(pagesize));
			siteItem.setCurrentRows(currentRows);
			request.setAttribute("siteItem", siteItem);
			request.setAttribute("siteName", siteName);
			/*for (SiteItem siteItem : itemsList) {
				System.out.println(siteItem.getITEMNAME());
			}*/
		}else{
			request.setAttribute("tips", "数据错误，请返回重试");
			return "permatters/error";
		}
		//为维护系统添加的type和types，后续可以删除
		request.setAttribute("type", request.getParameter("type"));
		request.setAttribute("itemsList", pageItemsList);
		return "permatters/siteItemList";
	}

	
}
